package com.atguigu.edu.realtime.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Author zhangsan
 * @Date 2022/10/19 19:07
 * @Description //TODO 操作Redis的工具类
 */
public class RedisUtil {
    private static JedisPool jedisPool;

    static {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(100); //最大连接数
        poolConfig.setMaxIdle(5); //最大空闲连接
        poolConfig.setMinIdle(5); //最小空闲连接
        poolConfig.setBlockWhenExhausted(true);  //资源耗尽 是否等待
        poolConfig.setMaxWaitMillis(2000);      //等待时间
        poolConfig.setTestOnBorrow(true);  //测试
        jedisPool = new JedisPool(poolConfig,"hadoop104",6379,10000);
    }

    //获取操作redis的java客户端对象jedis
    public static Jedis getJedis(){
        System.out.println("~~获取Jedis客户端~~");
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }

    public static void main(String[] args) {
        Jedis jedis = getJedis();
        String pong = jedis.ping();
        System.out.println(pong);
    }
}
